package com.act;

import java.util.ArrayList;
import java.util.UUID;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bean.BaseBean;
import com.common.Constant;
import com.common.TextHttpResponseHandler;
import com.xike.kyt.R;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

public class RegisterAct extends BaseAct implements OnClickListener {

	EditText edt_userName, edt_pwd, edt_checkCode;
	TextView tv_checkCode;
	Button btn_register;
	LinearLayout layout_agreement;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.act_register);
		initViews();
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initViews() {
		edt_userName = (EditText) findViewById(R.id.register_edt_phoneNum);
		edt_pwd = (EditText) findViewById(R.id.register_edt_password);
		edt_checkCode = (EditText) findViewById(R.id.register_edt_checkCode);

		tv_checkCode = (TextView) findViewById(R.id.register_tv_checkCode);
		tv_checkCode.setOnClickListener(this);
		tv_checkCode.setText(getRandomString());

		btn_register = (Button) findViewById(R.id.register_btn_register);
		btn_register.setOnClickListener(this);

		layout_agreement = (LinearLayout) findViewById(R.id.register_layout_agreement);
		layout_agreement.setOnClickListener(this);
		
		btn_register.setOnClickListener(this);
		findViewById(R.id.headTitle_tv_back).setOnClickListener(this);

	}

	@Override
	protected void updateViews() {
	}

	private void serviceRegister() {
		String uuid = UUID.randomUUID().toString().replace("-", "");
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("user_name", MyUtil.getEditTextContent(edt_userName)));
		params.add(new BasicNameValuePair("password", StringUtil.MD5(MyUtil.getEditTextContent(edt_pwd) + uuid)));
		params.add(new BasicNameValuePair("uuid", uuid));

		String strUrl = MyUtil.signRequestUrl(Constant.SERVER_URL_REGISTER, params);
		client.get(this, strUrl, new TextHttpResponseHandler(this) {

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mToast.showToast(RegisterAct.this, getString(R.string.net_error));
				super.onFailure(statusCode, headers, responseString, throwable);
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				BaseBean result = gson.fromJson(responseString, BaseBean.class);
				dealRequestResult(result);
				super.onSuccess(statusCode, headers, responseString);
			}

		});
	}

	private void dealRequestResult(BaseBean result) {
		if (result == null) {
			mToast.showToast(RegisterAct.this, getString(R.string.net_error));
		} else {
			if (result.isReturnTrue()) {
				// 注册成功
				mToast.showToast(RegisterAct.this, "注册成功");
				if (LoginAct.instance != null) {
					LoginAct.instance.finish();
				}
				Intent it = new Intent(this, LoginAct.class);
				it.putExtra("userName", MyUtil.getEditTextContent(edt_userName));
				it.putExtra("isNewLogin", true);
				startActivity(it);
				finish();
			} else {
				mToast.showToast(RegisterAct.this, result.getError_description());
			}
		}
	}

	private boolean checkInput() {
		String strPhoneNum = MyUtil.getEditTextContent(edt_userName);
		if (!StringUtil.isNotNull(strPhoneNum)) {
			mToast.showToast(this, "请输入手机号");
			return false;
		}

		if (!MyUtil.isPhoneNumberValid(strPhoneNum)) {
			mToast.showToast(this, "请输入有效的手机号");
			return false;
		}
		String strPwd = MyUtil.getEditTextContent(edt_pwd);
		if (!StringUtil.isNotNull(strPwd)) {
			mToast.showToast(this, "请输入登录密码");
			return false;
		}

		if (strPwd.length() < 6 || strPwd.length() > 30) {
			mToast.showToast(this, "密码长度为6-30位");
			return false;
		}

		if (!strPwd.matches("[a-zA-Z0-9]+")) {
			mToast.showToast(this, "密码只能含有数字和字母");
			return false;
		}
		if (strPwd.matches("[0-9]+") || strPwd.matches("[a-zA-Z]+")) {
			mToast.showToast(this, "密码必须包含数字和字母");
			return false;
		}

		String strInputCheckCode = MyUtil.getEditTextContent(edt_checkCode);
		if (!StringUtil.isNotNull(strInputCheckCode)) {
			mToast.showToast(this, "请输入验证码");
			return false;
		}

		String strCheckCode = tv_checkCode.getText().toString();
		if (!strCheckCode.equals(strInputCheckCode)) {
			mToast.showToast(this, "您输入的验证码有误，请重新输入");
			return false;
		}

		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.register_tv_checkCode:
			tv_checkCode.setText(getRandomString());
			break;
		case R.id.register_btn_register:
			if (checkInput()) {
				serviceRegister();
			}
			break;
		case R.id.register_layout_agreement:
			startActivity(new Intent(this, AgreementAct.class));
			break;
		case R.id.headTitle_tv_back:
			finish();
			break;

		default:
			break;
		}
	}

	private String getRandomString() {
		long num = (long) (Math.random() * 9000 + 1000);
		return String.valueOf(num);
	}

}
